<!DOCTYPE html>
<html>

<head lang="en">
    <title>骡窝日报</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="/js/jquery/jquery.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/news.css"/>
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/plugins/jrender/jrender.js"></script>
    <script src="/js/common.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/bootstrap/js/bootstrap.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <script src="/js/plugins/dialog/dialog.min.js"></script>

</head>

<body>


<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="index.html">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="搜索资讯">
            </div>
        </div>
    </div>
</div>

<div class="container news">

    <div class="news" id="ribao">
        <h6 class="news-title">骡窝日报 </h6>
        <div class="row" render-loop="list">
            <div class="col-6">
                <a data-id="2" render-fun="handler" render-key="list.id">
                    <img class="rounded" width="100%" height="110px"
                         render-src="list.coverUrl">
                </a>
                <div class="caption">
                    <p>
                        <span render-html="list.schedule" style="font-size:20px"></span>
                    </p>
                    <a render-fun="handler" render-key="list.id">
                        <p render-html="list.title" style="font-size:20px"></p>

                    </a>
                    <button class="btn btn-success  ibtn" render-value="list.id">收藏</button>

                </div>
                <p></p>
            </div>

        </div>
    </div>
</div>
<script>
    $(function () {
        $.get("/dailys", {sign: 1}, function (data) {
            renderVal(data);
            //获取日报ID
           /* $.each($(".ibtn"),function (i,n) {
                console.log(n);
                console.log(n.value);
            });*/
            $.get("/users/"+getUser().id+"/dailyIds",function (data) {
                $(".ibtn").each(function (e1, v1) {
                    var ribao = v1.value
                    $.each(data , function (e2, v2) {
                        if (ribao == v2){
                            $(v1).html("已收藏");
                            $(v1).addClass("btn-danger");

                        }
                    })

                })
            })


        });

        function renderVal(data) {
            $("#ribao").renderValues(data, {
                handler: function (ele, value) {
                    ele.href = "dailyContent.html?id=" + value;
                }
            });

            //绑定收藏按钮
            $(".ibtn").click(function () {

                var user = getUser();
                //日报ID
                var th = this;
                var dailyId = $(this).val();

                //判断用户是否登录
                if (user) {

                    //查询用户是否有收藏此日报,有的话就是取消收藏
                    $.get("/users/"+getUser().id+"/daily/collect",{dailyId:dailyId},function (data) {
                        if (!data.success){
                            $(document).dialog({
                                content: '已添加到我的收藏',
                            });

                            //加入收藏
                            $.post("/users/collect", {userId: user.id, dailyId: dailyId}, function (data) {
                                $(th).html("已收藏").addClass("btn-danger");
                            })
                        }else {
                            $(th).html("收藏").removeClass("btn-danger");
                            $(document).dialog({
                                content: '已删除收藏',
                            });

                            $.ajax({//删除收藏
                                type: "POST",
                                url: "/users/daily/collect",
                                //data: "_method=DELETE&dailyId="+dailyId+"&userId="+userId+"",
                                data: {_method: "DELETE", dailyId:dailyId, userId: getUser().id},
                                success: function (msg) {
                                }
                            });
                        }
                    })

                } else {//没有登录不让收藏
                    $(document).dialog({
                        type : 'confirm',
                        closeBtnShow: true,
                        content: '亲!您还未登录,点击确定按钮将回到登录页面',
                        onClickConfirmBtn: function(){
                            window.location="/mine/simpleProfiles.html"
                        },
                        onClickCancelBtn : function(){
                        }

                    });

                }
            })
        }

    })
</script>

</body>
</html>